package com.dc.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dc.common.ResponseResultVO;
import com.dc.entity.SysMessage;
import com.dc.entity.SysUser;
import com.dc.entity.ro.SystemMessageRO;
import com.dc.mapper.SysMessageMapper;
import com.dc.mapper.SysUserMapper;
import com.dc.service.SystemMessageService;
import com.dc.util.SnowflakeIdWorker;
import com.dc.util.UserUtil;
import com.dc.websocket.WebSocketServer;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SystemMessageServiceImpl implements SystemMessageService {

    @Resource
    SysMessageMapper sysMessageMapper;

    @Resource
    SysUserMapper sysUserMapper;

    @Resource
    WebSocketServer webSocketServer;


    @Override
    public ResponseResultVO selectSystemMessage(SystemMessageRO systemMessage) {
        IPage<SysMessage> iPage = new Page<>(systemMessage.getCurrentPage(), systemMessage.getPageSize());
        QueryWrapper<SysMessage> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNotNull("send_version");
        queryWrapper.groupBy("send_version");
        queryWrapper.orderByDesc("create_time");
        IPage<SysMessage> sysMessageDTOIPage = sysMessageMapper.selectPage(iPage, queryWrapper);
        for (SysMessage dto : sysMessageDTOIPage.getRecords()) {
            String userName = sysMessageMapper.selectUserName(dto.getUserId());
            dto.setUserName(userName);
        }
        return ResponseResultVO.resultList(sysMessageDTOIPage, systemMessage);
    }

    @Override
    public ResponseResultVO insertSystemMessage(SystemMessageRO systemMessage) {
        SysMessage messageDTO = new SysMessage();
        BeanUtils.copyProperties(systemMessage, messageDTO);
        messageDTO.setId(SnowflakeIdWorker.generateId());
        return ResponseResultVO.insertResult(sysMessageMapper.insert(messageDTO));
    }

    @Override
    public ResponseResultVO insertBatchSystemMessage(SystemMessageRO systemMessage) {
        if (StringUtils.isEmpty(systemMessage.getMessageContent())) {
            return ResponseResultVO.failResult("内容为空，请编辑内容");
        }

        SysUser sysUserDTO = sysUserMapper.selectById(UserUtil.getUserId());

        systemMessage.setCreateBy(sysUserDTO.getUserName());
        systemMessage.setSendVersion(IdUtil.fastUUID());
        systemMessage.setId(SnowflakeIdWorker.generateId());

        if (StringUtils.isEmpty(systemMessage.getType())) {

            Map<String, String> param = new HashMap<String, String>();
            param.put("msg", systemMessage.getMessageContent());
            //JPush.sendAll(param);

            //插入系统公告
            systemMessage.setMessageType(1);
            systemMessage.setType("0");
            int flag = sysMessageMapper.insertBatchSystemMessage(systemMessage);
            return ResponseResultVO.insertResult(flag);
        } else if ("1".equals(systemMessage.getType())) { //推送给手机用户
            //插入系统公告
            systemMessage.setMessageType(1);
            systemMessage.setType("1");
            if (systemMessage.getUserIds() == null || systemMessage.getUserIds().size() == 0) {
                return ResponseResultVO.failResult("用户为空");
            }
            int flag = sysMessageMapper.insertPdaUserBatchSystemMessage(systemMessage);
            return ResponseResultVO.insertResult(flag);

        } else {
            return ResponseResultVO.failResult("系统消息添加失败");
        }
    }

    @Override
    public ResponseResultVO deleteBatchSystemMessage(SystemMessageRO systemMessage) {
        String sendVersion = systemMessage.getSendVersion();
        if (StringUtils.isEmpty(sendVersion)) {
            return ResponseResultVO.failResult("请填写正确参数");
        }
        return ResponseResultVO.deleteResult(sysMessageMapper.delete(new QueryWrapper<SysMessage>().eq("send_version", systemMessage.getSendVersion())));
    }


    @Override
    public ResponseResultVO sendWebsocketMessage(String content, List<String> userIds) {
        try {
            if (CollectionUtils.isEmpty(userIds)) {
                webSocketServer.sendInfo(content);
            } else {
                webSocketServer.sendInfoToList(content, userIds);
            }
            return ResponseResultVO.successResult("发送成功");
        } catch (Exception exception) {
            exception.printStackTrace();
            return ResponseResultVO.failResult("发送失败");
        }

    }

}
